package com.tencent.qcloud.timchat.model;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.item.ChatQABean;
import com.bxd.baodan.util.StringUtil;
import com.tencent.TIMElem;
import com.tencent.TIMFaceElem;
import com.tencent.TIMMessage;
import com.tencent.TIMTextElem;
import com.tencent.qcloud.timchat.adapters.ChatAdapter;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;

/**
 * 文本消息数据
 */
public class LeftListMessage extends LeftMessage {

    public SendListener listener;

    private ChatQABean bean;


    public LeftListMessage(ChatQABean bean, SendListener listener) {
        message = new TIMMessage();
        this.bean = bean;
        this.listener = listener;
    }

    /**
     * 在聊天界面显示消息
     *
     * @param viewHolder 界面样式
     * @param context    显示消息的上下文
     */
    @Override
    public void showMessage(ChatAdapter.ViewHolder viewHolder, Context context) {
        clearView(viewHolder);
        viewHolder.message_no_read_count.setVisibility(View.GONE);
        boolean hasText = false;

        Glide.with(context).load(viewHolder.toShowUrl).error(R.drawable.empty_avatar).into(viewHolder.leftAvatar);

        LinearLayout linearLayout = new LinearLayout(App.getContext());
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        if (!StringUtil.isEmpty(bean.content)) {
            TextView content = new TextView(App.getContext());
            content.setText(bean.content);
            content.setPadding(0, 10, 10, 10);
            content.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            content.setTextColor(App.getContext().getResources().getColor(R.color.black));
            linearLayout.addView(content);
        }
        if (bean == null || bean.question == null) {
            return;
        }
        for (int i = 0; i < bean.question.size(); i++) {
            final TextView tv = new TextView(App.getContext());
            final ChatQABean.QABean firstQABean = bean.question.get(i);
            tv.setText(firstQABean.title);



            tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

            tv.setPadding(0, 15, 10, 15);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            tv.setTextColor(App.getContext().getResources().getColor(R.color.green_lab));
            linearLayout.addView(tv);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.send(firstQABean);
                }
            });
        }


        getBubbleView(viewHolder).addView(linearLayout);
        showStatus(viewHolder);
    }

    /**
     * 获取消息摘要
     */
    @Override
    public String getSummary() {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < message.getElementCount(); ++i) {
            switch (message.getElement(i).getType()) {
                case Face:
                    TIMFaceElem faceElem = (TIMFaceElem) message.getElement(i);
                    byte[] data = faceElem.getData();
                    if (data != null) {
                        result.append(new String(data, Charset.forName("UTF-8")));
                    }
                    break;
                case Text:
                    TIMTextElem textElem = (TIMTextElem) message.getElement(i);
                    result.append(textElem.getText());
                    break;
            }

        }
        return result.toString();
    }

    /**
     * 保存消息或消息文件
     */
    @Override
    public void save() {

    }

    private static int getNumLength(int n) {
        return String.valueOf(n).length();
    }


    public static SpannableStringBuilder getString(List<TIMElem> elems, Context context) {
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
        for (int i = 0; i < elems.size(); ++i) {
            switch (elems.get(i).getType()) {
                case Face:
                    TIMFaceElem faceElem = (TIMFaceElem) elems.get(i);
                    int startIndex = stringBuilder.length();
                    try {
                        AssetManager am = context.getAssets();
                        InputStream is = am.open(String.format("emoticon/%d.gif", faceElem.getIndex()));
                        if (is == null) continue;
                        Bitmap bitmap = BitmapFactory.decodeStream(is);
                        Matrix matrix = new Matrix();
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        matrix.postScale(2, 2);
                        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                                width, height, matrix, true);
                        ImageSpan span = new ImageSpan(context, resizedBitmap, ImageSpan.ALIGN_BASELINE);
                        stringBuilder.append(String.valueOf(faceElem.getIndex()));
                        stringBuilder.setSpan(span, startIndex, startIndex + getNumLength(faceElem.getIndex()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        is.close();
                    } catch (IOException e) {

                    }
                    break;
                case Text:
                    TIMTextElem textElem = (TIMTextElem) elems.get(i);
                    stringBuilder.append(textElem.getText());
                    break;
            }

        }
        return stringBuilder;
    }

    public interface SendListener {
        void send(ChatQABean.QABean bean);
    }

}
